<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/9/27
 * Time: 9:13
 * From  http://www.jb51.net/article/123797.htm
 */
header("Content-Type:text/html;charset=utf-8");

/**
 * Class queue
 * @User: yfl
 * @Time: 2017年9月27日 09:14:40
 * @description:PHP实现的链式队列结构示例
 *
 */
require_once '../common_funtion.php';

class node
{
    public $nickName;
    public $next;
}

class queue
{
    protected $front;//头部
    protected $tail;//尾部
    protected $maxSize;//容量
    protected $next;//指针
    protected $len = 0;//长度

    public function __construct($maxSize)
    {
        $this->init($maxSize);

    }

    protected function init($maxSize)
    {
        $this->front = $this;
        $this->tail = $this;
        $this->maxSize = $maxSize;
    }

    // 入队操作
    public function inQ($nickName)
    {
        $node = new node();
        $node->nickName = $nickName;
        if ($this->len == $this->maxSize) {
            echo '队列已满';
        } else {
            $this->tail = $node;
            $this->tail->next = $node;
//            $this->next = $node;
            $this->len++;
            echo $node->nickName . '入队成功</br>';
        }
    }

    //出对操作
    public function outQ()
    {
        if ($this->len == 0) {
            echo '队列为空';
        } else {
            $p = $this->tail->next;
            $this->front->next = $p->next;
            $this->len--;
            echo $p->nickName . '出队成功</br>';
        }
    }
}

echo "**********入队操作******************</br>";
$q = new queue(5);
$q->inQ('入云龙');
$q->inQ('花和尚');
$q->inQ('青面兽');
$q->inQ('行者');
$q->inQ('玉麒麟');
$q->inQ('母夜叉');
echo "**********出队队操作******************</br>";
$q->outQ();
$q->outQ();
$q->outQ();
$q->outQ();
$q->inQ('操刀鬼');
$q->inQ('截江鬼');
$q->inQ('赤发鬼');
$q->outQ();